package com.jdz.gxq.info.admin.registration.service;

import java.util.List;
import java.util.Map;

import com.jdz.gxq.info.admin.registration.bo.Registration;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

public class RegistrationServiceImpl implements RegistrationService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * Registration检索分页用，查询记录条数
	 * @return
	 */
	public int getRegistrationsCount(String enterpriseName) {
		
		int totalCount = queryDAO.executeForObject("registration.getRegistrationsCount",
				enterpriseName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * Registration检索
	 * @return
	 */
	public List<Registration> getRegistrations(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("registration.getRegistrations", params);
	}
	
	/**
	 * 追加Registration
	 * @param registration
	 */
	public void insertRegistration(Registration registration) {
		
		updateDAO.execute("registration.insertRegistration", registration);
	}
	
	/**
	 * 更新Registration
	 * @param registration
	 */
	public void updateRegistration(Registration registration) {
		
		updateDAO.execute("registration.updateRegistration", registration);
	}
	
	/**
	 * 通过id得到registration
	 * @param id
	 * @return
	 */
	public Registration getRegistrationById(int id) {

		Registration registration = queryDAO.executeForObject("registration.getRegistrationById",
				id, Registration.class);
		
		return registration;
	}
	
	/**
	 * 删除Registration
	 * @param registration
	 */
	public void deleteRegistration(int id) {
		
		updateDAO.execute("registration.deleteRegistrationById", id);
	}
	
	/**
	 * 验证registrationName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkRegistrationName(Registration paramRegistration, int flag) {
		
		Registration registration = queryDAO.executeForObject("registration.getRegistrationByName",
				paramRegistration.getEnterpriseName(), Registration.class);

		if (registration == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (registration.getId() == paramRegistration.getId()) {
				return true;
			}
		}
		
		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}
	
	
}
